package com.yunhe.device.repository.base.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yunhe.common.util.StringUtil;
import com.yunhe.device.domain.base.PointDataType;
import com.yunhe.device.model.base.AnlogTypeQueryCondition;
import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @ClassName AnlogTypeDao
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/15 10:13
 * @Version 1.0
 **/
@Repository
public class AnlogTypeDao {

    @Autowired
    private Session session;

    public Page<PointDataType> getAnlogTypeByCondition(AnlogTypeQueryCondition anlogTypeQueryCondition) {
        StringBuffer cql=new StringBuffer();
        //拼接基本语句
        cql.append("MATCH (at:PointDataType) WHERE 1=1 and at.backType='01' ");
        //关键字查询
        if(!StringUtil.isEmpty(anlogTypeQueryCondition.getKeywords())){
            cql.append(" AND (at.name=~'.*"+anlogTypeQueryCondition.getKeywords()+".*'");
            cql.append(" or at.title=~'.*"+anlogTypeQueryCondition.getKeywords()+".*')");
        }
        //监测类型查询
        if(!StringUtil.isEmpty(anlogTypeQueryCondition.getType())){
            cql.append(" AND at.type='"+anlogTypeQueryCondition.getType()+"'");
        }
        int skip=(anlogTypeQueryCondition.getPage()-1)*anlogTypeQueryCondition.getSize();
        String queryEnd=" RETURN at" +
                "  order by at.createTime desc  SKIP "+skip+" LIMIT "+anlogTypeQueryCondition.getSize();
        String countEnd=" RETURN count(at)";

        //分页数据查询结果
        Iterable<PointDataType> anlogTypes = session.query(PointDataType.class,cql.toString()+queryEnd, Maps.newHashMap());
        //查询总数
        Integer total = session.queryForObject(Integer.class,cql.toString()+countEnd, Maps.newHashMap());

        //封装为分页类
        List<PointDataType> content= Lists.newArrayList(anlogTypes);
        Pageable pageable= PageRequest.of(anlogTypeQueryCondition.getPage()-1,anlogTypeQueryCondition.getSize());
        Page<PointDataType> page=new PageImpl<PointDataType>(content,pageable,total);
        return page;
    }

    public Page<PointDataType> findByEquipTypeName(String equipName, int page, int size) {
        StringBuffer cql=new StringBuffer();
        if(StringUtil.isEmpty(equipName)){
            //拼接基本语句
            cql.append("MATCH (at:PointDataType) WHERE 1=1 ");
        }else {
            cql.append("MATCH (et:DeviceType})-[:HAS_ANLOGTYPE]->(at:PointDataType) where 1=1");
            cql.append(" and et.name='"+equipName+"'");
        }
        cql.append("AND at.activity='1'");
        int skip=(page-1)*size;
        String queryEnd=" RETURN at" +
                "  order by at.createTime desc  SKIP "+skip+" LIMIT "+size;
        String countEnd=" RETURN count(at)";

        //分页数据查询结果
        Iterable<PointDataType> anlogTypes = session.query(PointDataType.class,cql.toString()+queryEnd, Maps.newHashMap());
        //查询总数
        Integer total = session.queryForObject(Integer.class,cql.toString()+countEnd, Maps.newHashMap());

        //封装为分页类
        List<PointDataType> content= Lists.newArrayList(anlogTypes);
        Pageable pageable= PageRequest.of(page-1,size);
        Page<PointDataType> page1=new PageImpl<PointDataType>(content,pageable,total);
        return page1;
    }
}
